package com.joysuch.wwyt.patrol.utils;

import cn.hutool.core.date.DateUtil;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.patrol.entity.PatrolCheckPlan;
import com.joysuch.wwyt.patrol.entity.dto.PatrolPlanCycleDto;
import com.joysuch.wwyt.patrol.enums.CycleType;
import com.joysuch.wwyt.patrol.enums.CycleUnit;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class StatisticsTableTitleUtil {


    private static final DateFormat mdFormatter = new SimpleDateFormat("M.dd");
    private static final DateFormat hmFormatter = new SimpleDateFormat("HH:mm");
    private static final DateFormat fullFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static void convertTableTitle(List<PatrolPlanCycleDto> patrolPlanCycleDtos, PatrolCheckPlan plan) {
        CycleType cycleType = CycleType.find(plan.getCycleType());
        String cycleUnit = plan.getCycleUnit();
        switch (cycleType){
            case SPECIFY:
                if (CycleUnit.W.getValue().equals(cycleUnit)) {
                    convertSpecify4Week(patrolPlanCycleDtos);
                }else if (CycleUnit.D.getValue().equals(cycleUnit)) {
                    convertSpecify4Day(patrolPlanCycleDtos);
                }else if (CycleUnit.M.getValue().equals(cycleUnit)) {
                    convertSpecify4Month(patrolPlanCycleDtos);
                }
                return;
            case CYCLE:
                if (CycleUnit.H.getValue().equals(cycleUnit)) {
                    // 周期性、按小时，表头与指定次数按天相同
                    convertSpecify4Day(patrolPlanCycleDtos);
                }else if (CycleUnit.D.getValue().equals(cycleUnit)) {
                    // 周期性、按天时，表头与指定次数按月相同
                    convertSpecify4Month(patrolPlanCycleDtos);
                }else if (CycleUnit.W.getValue().equals(cycleUnit)) {
                    convertCycle4Week(patrolPlanCycleDtos);
                }else if (CycleUnit.M.getValue().equals(cycleUnit)) {
                    // 周期性、按月，表头与周期性按周相同
                    convertCycle4Week(patrolPlanCycleDtos);
                }
                return;
            case SINGLE:
                convertSingle(patrolPlanCycleDtos);
                return;
        }
    }

    private static void convertSpecify4Week(List<PatrolPlanCycleDto> patrolPlanCycleDtos) {
        patrolPlanCycleDtos.forEach(each -> {
            Date startTime = each.getStartTime();
            int dayOfWeek = DateUtil.dayOfWeek(startTime);
            String formatTime = mdFormatter.format(startTime);
            String title = new StringBuilder().append(formatTime)
                    .append(Constant.EN_LEFT_BRACKETS)
                    .append(getWeekOfInt(dayOfWeek))
                    .append(Constant.EN_RIGHT_BRACKETS)
                    .toString();
            each.setTitle(title);
        });
    }

    private static void convertSpecify4Day(List<PatrolPlanCycleDto> patrolPlanCycleDtos) {
        patrolPlanCycleDtos.forEach(each -> {
            Date startTime = each.getStartTime();
            Date endTime = each.getEndTime();
            String subStart = hmFormatter.format(startTime);
            String subEnd = hmFormatter.format(endTime);
            boolean sameDay = DateUtil.isSameDay(startTime, endTime);
            each.setTitle(subStart + Constant.SPLIT_HYPHEN + subEnd + (sameDay ? "":"(次日)"));
            Integer dayOfMonth = DateUtil.dayOfMonth(startTime);
            each.setSuperTitle(dayOfMonth.toString());
        });
    }

    private static void convertSpecify4Month(List<PatrolPlanCycleDto> patrolPlanCycleDtos) {
        patrolPlanCycleDtos.forEach(each -> {
            Date startTime = each.getStartTime();
            String formatTime = mdFormatter.format(startTime);
            each.setTitle(formatTime);
        });
    }

    private static void convertCycle4Week(List<PatrolPlanCycleDto> patrolPlanCycleDtos) {
        patrolPlanCycleDtos.forEach(each -> {
            Date startTime = each.getStartTime();
            Date endTime = each.getEndTime();
            String subStart = mdFormatter.format(startTime);
            String subEnd = mdFormatter.format(endTime);
            each.setTitle(subStart + Constant.SPLIT_HYPHEN + subEnd);
        });
    }

    private static void convertSingle(List<PatrolPlanCycleDto> patrolPlanCycleDtos) {
        patrolPlanCycleDtos.forEach(each -> {
            Date startTime = each.getStartTime();
            Date endTime = each.getEndTime();
            String subStart = fullFormatter.format(startTime);
            String subEnd = fullFormatter.format(endTime);
            each.setTitle(subStart + Constant.SPLIT_HYPHEN + subEnd);
            Integer dayOfMonth = DateUtil.dayOfMonth(startTime);
            each.setSuperTitle(dayOfMonth.toString());
        });
    }

    private static String getWeekOfInt(int week){
        switch (week){
            case 1:
                return "周日";
            case 2:
                return "周一";
            case 3:
                return "周二";
            case 4:
                return "周三";
            case 5:
                return "周四";
            case 6:
                return "周五";
            case 7:
                return "周六";
        }
        return "";
    }

}
